package com.hghivln.controller.system;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.pojo.dto.UserDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.UserService;
import com.hghivln.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("system/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     */
    @GetMapping("listUserForPage")
    public AjaxResult listAllUsers(UserDto userDto) {
        return userService.listUserForPage(userDto);
    }

    /**
     * 添加
     */
    @PostMapping("addUser")
    @Log(title = "添加用户",businessType = BusinessType.INSERT)
    public AjaxResult addUser(@Validated UserDto userDto) {
        System.out.println(userDto);
        userDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return userService.addUser(userDto);
    }

    /**
     * 修改
     */
    @PutMapping("updateUser")
    @Log(title = "修改用户",businessType = BusinessType.UPDATE)
    public AjaxResult updateUser(@Validated UserDto userDto) {
        userDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return userService.updateUser(userDto);
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteUserByIds/{ids}")
    @Log(title = "删除用户",businessType = BusinessType.DELETE)
    public AjaxResult deleteUserById(@PathVariable @Validated @NotNull(message = "要删除的用户ID不能为空") Long[] ids) {
        return userService.deleteUserByIds(ids);
    }

    /**
     * 根据ID查询一条用户信息
     */
    @GetMapping("getUserById/{id}")
    public AjaxResult getUserById(@PathVariable @Validated @NotNull(message = "用户ID不能为空") Long id) {
        return userService.getUserById(id);
    }

    /**
     * 重置密码
     */
    @PutMapping("resetPwd/{userIds}")
    @Log(title = "重置密码",businessType = BusinessType.UPDATE)
    public AjaxResult resetPwd(@PathVariable @Validated @NotEmpty(message = "重置密码的用户Id不能为空") Long[] userIds) {
        return userService.resetPwd(userIds);
    }

    /**
     * 查询所有需要排班的用户数据
     */
    @GetMapping("selectAllUser")
    public AjaxResult selectNeedSchedulingUsers() {
        return userService.selectAllUser();
    }

}
